package wiki.zimo.servlet.reception;

import wiki.zimo.constant.Constant;
import wiki.zimo.dao.ProductDao;
import wiki.zimo.domain.Product;
import wiki.zimo.servlet.BaseServlet;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.SQLException;
import java.util.List;

/**
 * @version v1.0
 * @auther: 子墨
 * @datetime: 2018/11/8 18:34
 * @description: 搜索商品
 */
@WebServlet(value = "/search")
public class SearchServlet extends BaseServlet {

    ProductDao productDao = new ProductDao();

    public String searchProduct(HttpServletRequest request, HttpServletResponse response) throws SQLException {

        String cid = request.getParameter("cid");
        String key = request.getParameter("key");

        List<Product> products = null;
        if (cid.equalsIgnoreCase("all")) {
            // 所有商品
            products = productDao.searchProduct("%" + key + "%", cid, false);
        } else {
            // 分类商品
            products = productDao.searchProduct("%" + key + "%", cid, true);
        }
        request.setAttribute(Constant.PRODUCTS, products);
        return Constant.URL_PRODUCT_SEARCH;
    }
}
